package logic

import (
	"context"
	"fmt"
	"post/docker/cache"
	"post/docker/mysql"
	"post/postrpc/internal/svc"
	"post/postrpc/postrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *postrpc.RegisterRequest) (*postrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Debug().Where("mobile=?", in.Mobile).Find(&user)
	if user.ID != 0 {
		return nil, fmt.Errorf("手机号已存在,请前去注册")
	}
	gets := cache.Rdb.Get(l.ctx, "sendSms").Val()
	if gets != in.SendSmsCode {
		return nil, fmt.Errorf("短信验证码错误")
	}
	m := mysql.User{

		Name:     in.Name,
		Mobile:   in.Mobile,
		Password: in.Password,
		Image:    in.Image,
	}
	mysql.DB.Debug().Create(&m)
	return &postrpc.RegisterResponse{
		UserId: int64(m.ID),
	}, nil

}
